import requests
import random
import redis

def get_urls():
    client = redis.StrictRedis(host='localhost', port=6379, db=0)
    if client.exists('zlpq'):
        url = client.lpop('urls')
    else:
        print('爬取结束')
        return None
    client.close()
    return url.decode()

def have_url(url):
    client = redis.StrictRedis(host='localhost', port=6379, db=0)
    client.lpush('urls',url)
    client.close()


def main():
    try:
        url = get_urls()
        if url is not None:
            res = requests.get(url,timeout=3)
            print(res.json())
        else:
            return '没有目标url'

    except Exception as e:
        print(f'{url}爬取失败 错误原因{e}')
        have_url(url)

for i in range(100):
    if main() == '没有目标url':
        break
    else:
        main()